package faq

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/dao/faq"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

type operateFaqParms struct {
	Id           int64 `form:"id"`
	DepartmentId int64 `form:"department_id"`
	Order        int   `form:"order"`
	CurPage      int   `form:"cur_page"`
	PageSize     int   `form:"page_size"`
}

type faqParms struct {
	Id             int64  `form:"id"`
	ContentLink    string `form:"content_link" binding:"required"`
	Title          string `form:"title" binding:"required"`
	Order          int    `form:"order"`
	DepartmentId   int64  `form:"department_id"`
	DepartmentName string `form:"department_name"`
}

/**
获取faq列表
*/
func GetFaq(c *gin.Context) {
	reqParam := operateFaqParms{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("参数错误", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if reqParam.PageSize < 0 || reqParam.CurPage < 0 {
		global.Log.Sugar().Error("页码值非法")
		c.JSON(http.StatusOK, model.Fail("页码值非法，只可为正数"))
		return
	}
	count, listValue, err := faq.GetFaq(reqParam.Id,
		reqParam.DepartmentId,
		reqParam.CurPage,
		reqParam.PageSize)

	if err != nil {
		global.Log.Sugar().Error("获取常见问题列表失败", err)
		c.JSON(http.StatusOK, err)
		return
	}
	c.JSON(http.StatusOK, model.SuccessGrid(count, listValue))
}

/**
设置faq
*/
func PutFaq(c *gin.Context) {
	reqParam := faqParms{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("参数错误", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	f := faq.Faq{}
	f.Id = reqParam.Id
	f.ContentLink = reqParam.ContentLink
	f.Title = reqParam.Title
	f.Department = reqParam.DepartmentName
	f.DepartmentId = reqParam.DepartmentId
	f.Order = reqParam.Order
	err := faq.PutFaq(&f)
	if err != nil {
		global.Log.Sugar().Error("设置常见问题失败", err)
		c.JSON(http.StatusOK, model.Fail("操作失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

/**
删除常见问题
*/
func DelFaq(c *gin.Context) {
	reqParam := operateFaqParms{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("参数错误", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if err := faq.DelFaq(reqParam.Id); err != nil {
		global.Log.Sugar().Error("删除常见问题失败", err)
		c.JSON(http.StatusOK, err)
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}
